package com.cyandpos.pad.tradedetails.model;

import android.text.TextUtils;
import android.util.Log;


import com.cyandpos.pad.common.commondata.BaseResponse;
import com.cyandpos.pad.common.netmanager.PosHttpManager;
import com.cyandpos.pad.http.OkHttpUtils;
import com.cyandpos.pad.posdata.Order;
import com.cyandpos.pad.posdata.QueryOrderReq;
import com.cyandpos.pad.posdata.RefundOrder;
import com.google.gson.JsonObject;

import java.util.List;

public class TradeDetailsModelImpl implements TradeDetailsModel {
    @Override
    public void loadTradeDetailsConfig(QueryOrderReq req, final OnLoadTradeDetailsListener listener) {
        //发起网络请求 成功回调
        PosHttpManager.QueryOrderInfoEexcute(req,new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject response) {

                Log.i("TAG-----订单查询",response.toString());

                try {
                    BaseResponse<RefundOrder> baseResponse = BaseResponse.fromJson(response.toString(), RefundOrder.class);
                    if (baseResponse.getCode().equals("0")){

                        listener.onLoadTradeDetailsSuccess(baseResponse.getData().getOrderList(),baseResponse.getData().getOrderCount(),baseResponse.getData());
                    }else {
                        if (TextUtils.isEmpty(baseResponse.getTips())){
                            onFailure(baseResponse.getMsg());
                        }else {
                            onFailure(baseResponse.getTips());
                        }
                    }
                }catch (Exception e){
                    onFailure("数据解析失败"+e.toString());
                }
            }

            @Override
            public void onFailure(String msg) {
                listener.onLoadTradeDetailsFailure(msg);
            }
        });
    }

    public interface OnLoadTradeDetailsListener{
        void onLoadTradeDetailsSuccess(List<Order> orderList, double orderCount, RefundOrder data);
        void onLoadTradeDetailsFailure(String errorMsg);

    }

}
